 package sza.oracle.apps.ap.oie.webui;
 
 import com.sun.java.util.collections.HashMap;
 import com.sun.java.util.collections.Hashtable;
 import java.io.PrintStream;
 import java.io.Serializable;

import java.sql.CallableStatement;
import java.sql.Types;

import java.util.Enumeration;
import java.util.List;

import oracle.apps.ap.oie.webui.CashAndOtherListCO;
 import oracle.apps.ap.oie.webui.NavigationUtility;
 import oracle.apps.fnd.common.VersionInfo;
 import oracle.apps.fnd.framework.OAApplicationModule;
 import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.webui.OADataBoundValueFireActionURL;
 import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.OAWebBeanConstants;
import oracle.apps.fnd.framework.webui.OAWebBeanFactory;
import oracle.apps.fnd.framework.webui.beans.OAImageBean;
import oracle.apps.fnd.framework.webui.beans.OATipBean;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;

import oracle.apps.fnd.framework.webui.beans.layout.OADefaultSingleColumnBean;
import oracle.apps.fnd.framework.webui.beans.layout.OAHeaderBean;
import oracle.apps.fnd.framework.webui.beans.layout.OAMessageComponentLayoutBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageChoiceBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageDateFieldBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageLovChoiceBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageLovInputBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageTextInputBean;

import oracle.apps.fnd.framework.webui.beans.table.OATableBean;

import oracle.jbo.Row;
import oracle.jbo.server.DBTransaction;

import sza.oracle.apps.ap.oie.utility.SzaOIEUtil;
 
 public class SzaCashAndOtherListCO extends CashAndOtherListCO
 {
   public static final String RCS_ID = "$Header$";
   public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header$", "%packagename%");
 
   public void processRequest(OAPageContext pageContext, OAWebBean webBean)
   {
     String LogMessage = "";
     String LogFunction = "";
     LogFunction = "SZA SzaCashAndOtherListCO.processRequest";
 
     LogMessage = "SZA SzaCashAndOtherListCO.processRequest - Start";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     super.processRequest(pageContext, webBean);
     
     /*
      * 差旅费优化项目修改
      * */
     OAApplicationModule oaapplicationmodule = pageContext.getRootApplicationModule(); 

       OAViewObject HeaderVo = (OAViewObject)oaapplicationmodule.findViewObject("ExpenseReportHeadersVO");
       String expenseReportid = HeaderVo.first().getAttribute("ExpenseReportId")+"";
       String reportHeaderId = HeaderVo.first().getAttribute("ReportHeaderId")+"";
       String flexConcatenated = HeaderVo.first().getAttribute("FlexConcatenated")+"";
     
       OAImageBean oaTravelBean = (OAImageBean)webBean.findIndexedChildRecursive("SzaOieTravelDetail");
       OAImageBean oAImageBean = (OAImageBean)webBean.findIndexedChildRecursive("SzaRBExpenseTypeTree");
       OAMessageDateFieldBean dateInput = (OAMessageDateFieldBean)webBean.findIndexedChildRecursive("Date");
       OAMessageTextInputBean justiInput = (OAMessageTextInputBean)webBean.findIndexedChildRecursive("Justification");
       OAMessageTextInputBean expenseType = (OAMessageTextInputBean)webBean.findIndexedChildRecursive("SzaExpenseType");
       
       OAMessageTextInputBean amountInput = (OAMessageTextInputBean)webBean.findIndexedChildRecursive("ReceiptCurrencyAmount");
       OAMessageLovInputBean projectLov = (OAMessageLovInputBean)webBean.findIndexedChildRecursive("SzaOieProjectLovRN");
       OAMessageLovInputBean expProjectLov = (OAMessageLovInputBean)webBean.findIndexedChildRecursive("SzaOieExpProjectLovRN");
       OAMessageLovInputBean taskLov = (OAMessageLovInputBean)webBean.findIndexedChildRecursive("SzaOieTaskRN");
       OAMessageLovInputBean costCenterLov = (OAMessageLovInputBean)webBean.findIndexedChildRecursive("SzaOieCostCenter");
       OAMessageChoiceBean  travelExpenseCategoryLov = (OAMessageChoiceBean )webBean.findIndexedChildRecursive("TravelExpenseCategoryRN");
//       OAHeaderBean header = (OAHeaderBean)webBean.findIndexedChildRecursive("OIEReceiptListContainer");
       OATableBean header = (OATableBean)webBean.findIndexedChildRecursive("OIECashAndOtherList");
        OAMessageComponentLayoutBean  parentWebBean;
        parentWebBean = (OAMessageComponentLayoutBean)webBean.findIndexedChildRecursive("ReceiptBasedRN");
        String returnValue = isTravleTemplate(expenseReportid,oaapplicationmodule);
        
     
      
     if("TRA".equals(returnValue)){
         pageContext.putSessionValue("ReportHeaderId",reportHeaderId);
         if(oAImageBean!=null){
           oAImageBean.setRendered(Boolean.FALSE);
           
         }
//         Enumeration e =  webBean.getChildNames();
       
//         header.setRendered(Boolean.FALSE);
//         createWebBean(pageContext, OAWebBeanConstants.HEAD_BEAN, null, "TraveExpHr");
//         OAWebBeanFactory factory = pageContext.getWebBeanFactory();
//         factory.createWebBean(pageContext, parentWebBean, "/sza/oracle/apps/ap/oie/traveExp/webui/TraveRN", "TraveExpHr", Boolean.TRUE);
         if(oaTravelBean!=null){
           oaTravelBean.setRendered(Boolean.FALSE);
         }
         projectLov.setRendered(Boolean.TRUE);
         taskLov.setRendered(Boolean.TRUE);
         travelExpenseCategoryLov.setRendered(Boolean.TRUE);
         expenseType.setRendered(Boolean.FALSE);
         
         OAWebBeanFactory factory = pageContext.getWebBeanFactory();
         OAHeaderBean headerBean = (OAHeaderBean)factory.createWebBean(pageContext, "/sza/oracle/apps/ap/oie/traveExp/webui/TraveRN", "TraveExpHr", Boolean.TRUE);
         if(headerBean == null){
             throw new OAException(headerBean.getID()+"  "+headerBean.isRendered());
         }
         webBean.addIndexedChild(0,headerBean);

     }else if("BUDGET".equals(returnValue)){
         pageContext.putSessionValue("ReportHeaderId",reportHeaderId);
         if(oAImageBean!=null){
           oAImageBean.setRendered(Boolean.FALSE);
           
         }
         if(oaTravelBean!=null){
           oaTravelBean.setRendered(Boolean.FALSE);
         }
         projectLov.setRendered(Boolean.TRUE);
         taskLov.setRendered(Boolean.TRUE);
//         costCenterLov.setRendered(Boolean.TRUE);
//         expenditureLov.setRendered(Boolean.TRUE);
//         travelExpenseCategoryLov.setRendered(Boolean.TRUE);
         expenseType.setRendered(Boolean.FALSE);
         
     }else if("EXP".equals(returnValue)){
         pageContext.putSessionValue("ReportHeaderId",reportHeaderId);
         if(oAImageBean!=null){
           oAImageBean.setRendered(Boolean.FALSE);
           
         }
         if(oaTravelBean!=null){
           oaTravelBean.setRendered(Boolean.FALSE);
         }
         expProjectLov.setRendered(Boolean.TRUE);
         taskLov.setRendered(Boolean.TRUE);
         costCenterLov.setRendered(Boolean.TRUE);
//         costCenterLov.setText(flexConcatenated);
         costCenterLov.setDefaultValue(flexConcatenated);
         //         expenditureLov.setRendered(Boolean.TRUE);
         //         travelExpenseCategoryLov.setRendered(Boolean.TRUE);
         expenseType.setRendered(Boolean.FALSE);
     
     } else {
         if(oaTravelBean!=null){
           oaTravelBean.setRendered(Boolean.FALSE);
         }
 
     if (oAImageBean != null)
     {
       String fireEvent = "RBExpenseTypeTree";
 
       String pageName = pageContext.getRootRegionCode();
       Hashtable params = new Hashtable(3);
       params.put("pageName", pageName);
 
       Hashtable paramsWithBinds = new Hashtable(3);
       paramsWithBinds.put("LineClicked", new OADataBoundValueFireActionURL(oAImageBean, "{$LineNumber}"));
 
       paramsWithBinds.put("CashDistLineNumber", new OADataBoundValueFireActionURL(oAImageBean, "{$DistributionLineNumber}"));
 
       paramsWithBinds.put("ReportLineId", new OADataBoundValueFireActionURL(oAImageBean, "{$ReportLineId}"));
 
       System.out.println("oAImageBean : oAImageBean");
     }
     }
 
     LogMessage = "SZA SzaCashAndOtherListCO.processRequest - End";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
   }
 
   public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
   {
     String LogMessage = "";
     String LogFunction = "";
     LogFunction = "SZA SzaCashAndOtherListCO.processFormRequest";
 
     LogMessage = "SZA SzaCashAndOtherListCO.processFormRequest - Start";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     super.processFormRequest(pageContext, webBean);
 
     OAApplicationModule oaapplicationmodule = pageContext.getRootApplicationModule();
 
     boolean flag = false;
 
     if (("RBExpenseTypeTree".equals(pageContext.getParameter("event"))) || ("RBExpenseTypeTree".equals(pageContext.getParameter("RBExpenseTypeTree"))))
     {
       System.out.println("2RBExpenseTypeTree");
       try
       {
         Serializable[] aserializable3 = { Boolean.FALSE };
 
         Class[] aclass2 = { Boolean.class };
 
         oaapplicationmodule.invokeMethod("updateLines", aserializable3, aclass2);
       }
       catch (OAException oaexception1)
       {
       }
 
       String CashDistLineNumber = pageContext.getParameter("ReportLineId");
       String ReportHeaderId = (String)pageContext.getTransactionValue("ReportHeaderId");
 
       Serializable[] aserializable7 = { Boolean.FALSE, CashDistLineNumber };
 
       Class[] aclass4 = { Boolean.class, String.class };
 
       Boolean boolean1 = (Boolean)oaapplicationmodule.invokeMethod("isReportLineValid", aserializable7, aclass4);
 
       HashMap hashmap = new HashMap();
       hashmap.put("SDP_RHID", NavigationUtility.encrypt(pageContext, ReportHeaderId));
 
       hashmap.put("SDP_RLID", NavigationUtility.encrypt(pageContext, CashDistLineNumber));
 
       CashDistLineNumber = pageContext.getParameter("CashDistLineNumber");
       hashmap.put("CashDistLineNumber", NavigationUtility.encrypt(pageContext, CashDistLineNumber));
 
       hashmap.put("SDP_EXP_CLASS", "SDP_EXP_CLASS_CASH");
 
       hashmap.put("SDP_UPDATE_MODE", "UPDATE");
 
       hashmap.put("SDP_CALLING_MODULE", "SZA_CASH_ET");
 
       hashmap.put("retainAM", "N");
 
       if (Boolean.FALSE.equals(boolean1)) {
         hashmap.put("SDP_SHOW_ERRORS", "SDP_SHOW_ERRORS");
       }
       if (pageContext.isLoggingEnabled(2)) {
         pageContext.writeDiagnostics("NavigationUtility.forwardToPage", "ExpenseTypeTreePG", 2);
       }
 
       SzaNavigationUtility.forwardToPage(pageContext, "ExpenseTypeTreePG", hashmap, Boolean.valueOf(true));
     } else if  (("TravelDetail".equals(pageContext.getParameter("event"))) || ("TravelDetail".equals(pageContext.getParameter("TravelDetail")))){
         System.out.println("TravelDetail");
         try
         {
           Serializable[] aserializable3 = { Boolean.FALSE };
         
           Class[] aclass2 = { Boolean.class };
         
           oaapplicationmodule.invokeMethod("updateLines", aserializable3, aclass2);
         }
         catch (OAException oaexception1)
         {
         }
         
         String CashDistLineNumber = pageContext.getParameter("ReportLineId");
         String ReportHeaderId = (String)pageContext.getTransactionValue("ReportHeaderId");
         
         Serializable[] aserializable7 = { Boolean.FALSE, CashDistLineNumber };
         
         Class[] aclass4 = { Boolean.class, String.class };
         
         Boolean boolean1 = (Boolean)oaapplicationmodule.invokeMethod("isReportLineValid", aserializable7, aclass4);
         
         HashMap hashmap = new HashMap();
         hashmap.put("SDP_RHID", NavigationUtility.encrypt(pageContext, ReportHeaderId));
         
         hashmap.put("SDP_RLID", NavigationUtility.encrypt(pageContext, CashDistLineNumber));
         
         CashDistLineNumber = pageContext.getParameter("CashDistLineNumber");
         hashmap.put("CashDistLineNumber", NavigationUtility.encrypt(pageContext, CashDistLineNumber));
         
         hashmap.put("SDP_EXP_CLASS", "SDP_EXP_CLASS_CASH");
         
         hashmap.put("SDP_UPDATE_MODE", "UPDATE");
         
         hashmap.put("SDP_CALLING_MODULE", "SZA_CASH_ET");
         
         hashmap.put("retainAM", "N");
         hashmap.put("exeInit","Y");
         
         if (Boolean.FALSE.equals(boolean1)) {
           hashmap.put("SDP_SHOW_ERRORS", "SDP_SHOW_ERRORS");
         }
        
         
         SzaNavigationUtility.forwardToPage(pageContext, "TravelDetailPG", hashmap, Boolean.valueOf(true));
     } else if(pageContext.isLovEvent()){
         if("SzaOieExptureTypeRN".equals(pageContext.getLovInputSourceId())){
             oaapplicationmodule.getTransaction().commit();
             String ReportHeaderId = (String)pageContext.getTransactionValue("ReportHeaderId");
             String exptureType = (String)pageContext.getParameter("SzaOieExptureTypeRN");
             System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@reportHeaderId:"+ReportHeaderId+"   exptureType:"+exptureType);
         }
     }
 
     LogMessage = "SZA SzaCashAndOtherListCO.processFormRequest - End";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
   }
   
   
   /*
    * 判断是否是差旅费模版
    * */
    private String isTravleTemplate(String TemplateId, OAApplicationModule am) {
        String returnValue = "";
        String callStmt = 
            " begin SZA_OIE_BUDGET_UTIL.GET_REPORT_TEMPLATE_TYPE(p_expense_report_id => :1,\n" + 
            "                          o_return_value       => :2); end;";
        DBTransaction txn = am.getOADBTransaction();
        CallableStatement cs = null;
        Row row = null;
      
            try {
                cs = txn.createCallableStatement(callStmt, 1);
                cs.setString(1, TemplateId);
                cs.setString(2, returnValue);
                cs.registerOutParameter(2, Types.VARCHAR);
                cs.execute();
                returnValue = cs.getString(2);
               
                cs.close();
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    cs.close();
                } catch (Exception ex) {

                }
            }
            return returnValue;

      
    }
 }



